import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Login from '@/pages/Login'
import List from '@/pages/List'
import Register from '@/pages/Register'
import Upload from '@/pages/Upload'
import browser from '@/pages/browser/browser.vue'
import welcome from '@/pages/home/welcome.vue'
import publish from '@/pages/publish/publish.vue'
import modify from '@/pages/Modify/modify.vue'
import success from '@/pages/Register/success.vue'
import aggrement from '@/pages/Register/aggrement.vue'
import forget from '@/pages/Forgetpass/forget.vue'
import Step1 from '@/pages/Forgetpass/Step1.vue'
import Step2 from '@/pages/Forgetpass/Step2.vue'
import Step3 from '@/pages/Forgetpass/Step3.vue'
import look from '@/pages/browser/look.vue'
import manage from '@/pages/Manager/manage.vue'
import m_home from '@/pages/Manager/m_home.vue'
import bmanage from '@/pages/Manager/bmanage.vue'
import usermanage from '@/pages/Manager/usermanage.vue'
import blog from '@/pages/user_home/blog.vue'
import home from '@/pages/user_home/home.vue'
import search from '@/pages/user_home/search.vue'
import blog2 from '@/pages/user_home/blog2.vue'
import type from '@/pages/user_home/type.vue'
import infoSide from '@/personal/infoSide.vue'
import Personal from '@/components/info-display.vue'
import Collect from '@/sub-page/collection-display.vue'
import Thumbs from '@/components/thumbsUp-dispaly.vue'
import Mycomment from '@/components/comment-display.vue'
import NOTFOUND from '@/components/not-found.vue'
import ColDetails from '@/components/coldetails.vue'
Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld,
      redirect: '/home'
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/list',
      name: 'List',
      component: List,
      redirect:'/welcome',
      children:[
        {path:'/welcome',component:welcome},
        {path:'/browser',component:browser},
        {path:'/publish',component:publish}
      ]
    },
    {
      path: '/register',
      name: 'Register',
      component: Register
    },
    {
      path: '/upload',
      name: 'Upload',
      component: Upload
    },
    {
      path: '/modify',
      name: 'modify',
      component: modify
    },
    {
      path: '/success',
      name: 'success',
      component: success
    },
    {
      path: '/aggrement',
      name: 'aggrement',
      component: aggrement
    },
    {
      path: '/forget',
      name: 'forget',
      component: forget,
      redirect:'/Step1',
      children:[
        {path:'/Step1',component:Step1},
        {path:'/Step2',component:Step2},
        {path:'/Step3',component:Step3}
      ]
    },
    {
      path:'/look',
      name: 'look',
      component: look
    },
    {
      path: '/manage',
      name: 'manage',
      component: manage,
      redirect:'/manage/home',
      children:[
        {path:'/manage/home',component:m_home},
        {path:'/manage/bmanage',component:bmanage},
        {path:'/manage/usermanage',component:usermanage}
      ]
    },
    {
      path: '/blog',
      component: blog
    },
    {
      path: '/home',
      component: home
    },
    {
      path: '/search',
      component: search
    },
    {
      path: '/blog2',
      component: blog2
    },
    {
      path: '/type',
      component: type
    },
    {
      path: '/infoside',
      name: 'infoside',
      component: infoSide,
      redirect:'/infoside/personal',
      children: [
        {
          path: '/infoside/personal',
          name: 'personal',
          component: Personal
        },
        {
          path: '/infoside/mycollect',
          name: 'mycollect',
          component: Collect,
          children: [
            {
              path: '/infoside/mycollect/details',
              name: 'coldetails',
              component: ColDetails
            }
          ]
        },
        {
          path: '/infoside/mythumbs',
          name: 'mythumbs',
          component: Thumbs
        },
        {
          path: '/infoside/mycomment',
          name: 'mycomment',
          component: Mycomment
        }

      ]
    },
    {
      path: '*',
      name: 'not found',
      component: NOTFOUND
    }
  ]
})


// 路由导航
router.beforeEach((to, from, next) => {
  //to and from are Route Object,next() must be called to resolve the hook}
  if(to.path === '/login'||to.path === '/register' || to.path === '/Step1' ||to.path === '/aggrement' || to.path === '/Step2' || to.path === '/Step3'||to.path ==='/home'||to.path ==='/search'||to.path ==='/blog2'||to.path ==='/type') 
    return next()
  //管理员token
  if(to.path === '/manage/home' || to.path === '/manage/bmanage' || to.path === '/manage/usermanage')
  {
    const admintoken = window.sessionStorage.getItem('admintoken')
    if(!admintoken) {
      return next('/login')
    }
    return next()
  }
  //查看博客
  if(to.path === '/blog'){
    const admintoken = window.sessionStorage.getItem('admintoken')
    const tokenStr = window.sessionStorage.getItem('token')
    if(admintoken != null || tokenStr !=null) {
      return next()
    }
    return next('/login')
  }
  //获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if(!tokenStr) 
  {
      return next('/login')
  }
  else next()
})

export default router